﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace SpellCreationTool
{
    public struct Spell
    {
        private int damage;
        private string name;
        private int elementID;
        private string animFilename;
        private int spellTypeID;

        [XmlAttribute("Name")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        [XmlAttribute("Damage")]
        public int Damage
        {
            get { return damage; }
            set { damage = value; }
        }

        [XmlAttribute("AnimFileName")]
        public string AnimFilename
        {
            get { return animFilename; }
            set { animFilename = value; }
        }

        [XmlAttribute("ElementID")]
        public int ElementID
        {
            get { return elementID; }
            set { elementID = value; }
        }

        [XmlAttribute("SpellTypeID")]
        public int SpellTypeID
        {
            get { return spellTypeID; }
            set { spellTypeID = value; }
        }

        public override string ToString()
        {
            return name;
        }
    }

    public partial class frmSpellEditor : Form
    {
        public frmSpellEditor()
        {
            InitializeComponent();
        }

        private void mnuFileSave_Click(object sender, EventArgs e)
        {
            dlgSaveFile.ShowDialog();

            string filename = dlgSaveFile.FileName;
            //create a list of spells
            List<Spell> mySpellList = new List<Spell>();

            for(int i = 0; i < lstSpellList.Items.Count; i++)
            {
                mySpellList.Add( (Spell)lstSpellList.Items[i]);
            }

            XmlSerializer serializer = new XmlSerializer(typeof(List<Spell>));
            TextWriter textWriter = new StreamWriter(filename);
            serializer.Serialize(textWriter, mySpellList);
            textWriter.Close();
        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {
     

            //will rework this later, quick and dirty method
            if (btnUpdate.Text == "Add")
            {
                //deselect any selected spells
                lstSpellList.ClearSelected();
                lstSpellList.Enabled = false;

                //clear the controls
                ClearControls();

                btnUpdate.Text = "Save";
                btnRemove.Text = "Cancel";
            }
            else if (btnUpdate.Text == "Save")
            {
                //grab the spell info
                Spell spell = new Spell();
                spell.AnimFilename = txtAnimFileName.Text;
                spell.Damage = Int32.Parse(txtDamage.Text);
                spell.ElementID = cmbSpellElement.SelectedIndex;
                spell.Name = txtSpellName.Text;
                spell.SpellTypeID = cmbSpellType.SelectedIndex;

                lstSpellList.Items.Add(spell);

                //change button back and clear controls
                btnUpdate.Text = "Add";
                ClearControls();
                lstSpellList.Enabled = true;
            }
            else if (btnUpdate.Text == "Edit")
            {
                int index = lstSpellList.SelectedIndex;

                Spell spell = new Spell();

                spell.AnimFilename = txtAnimFileName.Text;
                spell.Damage = Int32.Parse(txtDamage.Text);
                spell.ElementID = cmbSpellElement.SelectedIndex;
                spell.Name = txtSpellName.Text;
                spell.SpellTypeID = cmbSpellType.SelectedIndex;

                //remove the item
                lstSpellList.Items.RemoveAt(index);

                //insert the updated item
                lstSpellList.Items.Insert(index, spell);

                //change text
                btnUpdate.Text = "Add";
                btnRemove.Text = "Remove";
                ClearControls();
            }
        }

        public void ClearControls()
        {
            //clear everything
            foreach (Control c in frmSpellEditor.ActiveForm.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Clear();

                if (c is ComboBox)
                    ((ComboBox)c).SelectedIndex = -1;
            }
        }

        private void lstSpellList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSpellList.SelectedIndex != -1)
            {
                Spell selectedSpell = (Spell)lstSpellList.SelectedItem;

                //populate controls
                txtDamage.Text = selectedSpell.Damage.ToString();
                cmbSpellType.SelectedIndex = selectedSpell.SpellTypeID;
                txtAnimFileName.Text = selectedSpell.AnimFilename;
                txtSpellName.Text = selectedSpell.Name;
                cmbSpellElement.SelectedIndex = selectedSpell.ElementID;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            
            if (btnRemove.Text == "Cancel")
            {
                btnUpdate.Text = "Add";
                lstSpellList.Enabled = true;
                btnRemove.Text = "Remove";
            }
            else if (btnRemove.Text == "Remove")
            {
                lstSpellList.Items.RemoveAt(lstSpellList.SelectedIndex);
                lstSpellList.ClearSelected();
                ClearControls();
            }
        }

        private void lstSpellList_DoubleClick(object sender, EventArgs e)
        {
            btnUpdate.Text = "Edit";
            btnRemove.Text = "Cancel";
        }

        private void mnuFileOpen_Click(object sender, EventArgs e)
        {
            dlgOpenFile.ShowDialog();
            string filename = dlgOpenFile.FileName;

            List<Spell> spells = new List<Spell>();

            XmlSerializer deserializer = new XmlSerializer(typeof(List<Spell>));
            TextReader textReader = new StreamReader(filename);
            spells = (List<Spell>)deserializer.Deserialize(textReader);
            textReader.Close();

            foreach (Spell sp in spells)
            {
                lstSpellList.Items.Add(sp);
            }
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            dlgOpenFile.ShowDialog();
            txtAnimFileName.Text = dlgOpenFile.FileName;
        }

        private void frmSpellEditor_Load(object sender, EventArgs e)
        {
            MessageBox.Show("*****Instructions**********\n" +
                            "1. Click Add, then type the info for your spell.\n" +
                            "2. Double click an item in the spell list to edit it.\n" +
                            "3. This is not final, changes will be made, but the file structure should remain \n" +
                            "   so future versions 'should' be able to open previous files." +
                            "4. Save As currently does not work, use Save.  This will change." +
                            "\n\n That is it for now.  Watch for changes!!!!!!");
        }
    }
}
